package com.westvalley.plugin.db.xml;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.dialect.AnsiSqlDialect;
import com.jfinal.plugin.activerecord.dialect.Dialect;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;

@XmlRootElement(name="construct")
public class Construct {
	@XmlAttribute(name="id")
	public String className;
	@XmlElement(name = "set")
	public List<Set> sets = new ArrayList<Set>();
	
	public String getType(){
		return getAttrButeValue("type");
	}
	
	public String getDataSourceName(){
		return getAttrButeValue("datasourcename");
	}
	
	public String getIsCluster(){
		return getAttrButeValue("iscluster");
	}
	
	public String getTypeName(){
		return getAttrButeValue("typename");
	}
	
	public String getUrl(){
		return getAttrButeValue("url");
	}
	
	public String getHost(){
		return getAttrButeValue("host");
	}
	
	public String getPort(){
		return getAttrButeValue("port");
	}
	
	public String getDbName(){
		return getAttrButeValue("dbname");
	}
	
	public String getUser(){
		return getAttrButeValue("user");
	}
	
	public String getPassWord(){
		return getAttrButeValue("password");
	}
	
	public String getMinConn(){
		return getAttrButeValue("minconn");
	}
	
	public String getMaxConn(){
		return getAttrButeValue("maxconn");
	}
	
	public String getIsCode(){
		return getAttrButeValue("iscode");
	}
	
	public Dialect getDialect(){
		String type = getAttrButeValue("type");
		if("sqlserver".equals(type) || "sqlserver2005".equals(type) || "sqlserver2008".equals(type)){
			return new SqlServerDialect();
		}else if("oracle".equals(type)){
			return new OracleDialect();
		}else if("mysql".equals(type)){
			return new MysqlDialect();
		}
		return new AnsiSqlDialect();
	}
	
	private String getAttrButeValue(String key){
		if(sets.size()>0 && StrKit.notBlank(key)){
			for(Set set : sets){
				if(key.equals(set.property)){
					return set.value;
				}
			}
		}
		return "";
	}
}
